/**
 * Created by yanghaibo on 2016/10/25.
 */
ActivityCreateConsumeSmController = function () {

    // 渲染第一页数据
    this.redrawFirstStepView = function() {

        var activityName = Constants.activity.getActivityName(createActivityPublicParam.activityType);
        $("#smCreateConsumePage head title").html(activityName);
        $("#smCreateConsumePage .header .middle").html(activityName);

        var voucherList = createActivityPublicParam.otherSetting.promoTools;
        var nextFlag = false;// 下一步按钮
        var addFlag = true;// 添加优惠券按钮
        if(!$.isEmptyObject(voucherList) && voucherList.length>0){
            nextFlag = true;
        }
        if(!$.isEmptyObject(voucherList) && voucherList.length>=5){
            addFlag = false;
        }

        var crowdShow = false;
        if(createActivityPublicParam.activityType==ACTIVITY_TYPE.CROWD_SEND_FOR_CONSUME.val){
            crowdShow = true;
        }


        var otherSetting = createActivityPublicParam.otherSetting;
        var params = {
            "voucherList":voucherList,
            "nextFlag":nextFlag,
            "addFlag":addFlag,
            "crowdShow":crowdShow,
            "isRelativeTime":createActivityPublicParam.isRelativeTime,            
            "voucherRelativeTime":otherSetting.voucherRelativeTime.split("|")[0]==''?'30': otherSetting.voucherRelativeTime.split("|")[0],
            "voucherStartTime":createActivityPublicParam.voucherStartTime,
            "voucherEndTime":createActivityPublicParam.voucherEndTime

        };

        CommonUtil.render($("#smCreateConsumePage .page-body"), "firstStepCreateTPL", params);
    };


    // 渲染第二页数据
    this.redrawSecondStepView = function(data) {

        var self = this;

        var activityName = Constants.activity.getActivityName(createActivityPublicParam.activityType);
        $("#smCreateConsumePage head title").html(activityName);
        $("#smCreateConsumePage .header .middle").html(activityName);

        CommonUtil.render($("#smCreateConsumePage .page-body"), "secondStepCreateTPL", data);

        if(data.autoDelayFlag=="N"){
            $("#smCreateConsumePage .autoDelayFlag").prop("checked",false);
        }
        // 重新绑定时间控件的事件
        // 初始化日期控件;
        ActivityUtil.loadTimeWidget(true,function(){
            var activityDateParams = createActivityPublicParam.selectDateParams;
            activityDateParams.dateType = "activityDate";
            activityDateParams.pageId = "smCreateConsumePage";
            activityDateParams.beginDateInputId = "activityStartTime";
            activityDateParams.endDateInputId = "activityEndTime";
            activityDateParams.selectBeginDateFunc = function(selectTime){
                if(selectTime != createActivityPublicParam.activityStartTime){
                    createActivityPublicParam.activityStartTime = selectTime;
                    createActivityPublicParam.activityEndTime = DateUtil.getDateFormat(DateUtil.addDays(DateUtil.str2Date(selectTime),30));
                    $("#smCreateConsumePage #activityEndTime").val("");
                    ActivityUtil.loadTimeWidget(true);
                    //self.queryExpectEffect(false);
                }
            };
            activityDateParams.selectEndDateFunc = function(selectTime){
                if(selectTime != createActivityPublicParam.activityEndTime){
                    createActivityPublicParam.activityEndTime = selectTime;
                    ActivityUtil.loadTimeWidget(true);
                    self.queryExpectEffect(false);
                }       
            };
            
        });
    };


    // 渲染活动预期效果
    this.redrawExpectEffectView = function(data) {
         // 渲染模板
        CommonUtil.render($("#smCreateConsumePage .activity-expect-effect"), "expectEffectTPL", data);
    };

    // 获取活动预期效果
    this.queryExpectEffect = function(isDefault){
        var otherSetting = createActivityPublicParam.otherSetting;
        var defaultParam = createActivityPublicParam.defaultParam;
        var diagnoseDate = defaultParam.diagnoseDate;
        var diagnoseCode = defaultParam.diagnoseCode;
        var configCode = encodeURIComponent(defaultParam.configCode);
        var crowdGroupId = defaultParam.crowdGroupId;
        var crowdName = encodeURIComponent(defaultParam.crowdName);

        var serverUrl = App.serverUrl +　"merchantApp/smartMarketing/queryForecast.do?";
        serverUrl = serverUrl+"isDefault="+isDefault+"&diagnoseDate="
                +diagnoseDate+"&diagnoseCode="+diagnoseCode+"&configCode="+configCode;
        if($.isEmptyObject(isDefault)&&!isDefault){

            var activityStartTime = createActivityPublicParam.activityStartTime;
            var activityEndTime = createActivityPublicParam.activityEndTime;
            var voucherRelativeTime = otherSetting.voucherRelativeTime;

            var voucherWorthValue = "";
            var promoSendRuleMincost = "";
            var voucherMinConsume = "";
            var crowdGroup = "";
            var voucherType = "";
            $.each(otherSetting.promoTools,function(i,e) {
                if(CommonUtil.isEmpty(voucherWorthValue)){
                    voucherWorthValue = e.voucherWorthValue;
                    promoSendRuleMincost = e.promoSendRuleMincost;
                    voucherMinConsume = e.voucherMinConsume;
                    crowdGroup =  e.crowdGroup;
                    voucherType =  e.voucherType;
                }else{
                    voucherWorthValue = voucherWorthValue +"|"+e.voucherWorthValue;
                    promoSendRuleMincost = promoSendRuleMincost +"|"+e.promoSendRuleMincost;
                    voucherMinConsume = voucherMinConsume +"|"+e.voucherMinConsume;
                    crowdGroup = crowdGroup +"|"+e.crowdGroup;
                    voucherType = voucherType +"|"+e.voucherType;
                }

            });

            if(createActivityPublicParam.activityType!=ACTIVITY_TYPE.CROWD_SEND_FOR_CONSUME.val){
                voucherType = defaultParam.voucherType;
            }

            serverUrl = serverUrl
                        +"&activityStartTime="+activityStartTime
                        +"&activityEndTime="+activityEndTime
                        +"&voucherWorthValue="+voucherWorthValue
                        +"&promoSendRuleMincost="+promoSendRuleMincost
                        +"&voucherMinConsume="+voucherMinConsume
                        +"&voucherRelativeTime="+voucherRelativeTime//TODO
                        +"&voucherType="+voucherType
                        ;
             if(createActivityPublicParam.activityType==ACTIVITY_TYPE.CROWD_SEND_FOR_CONSUME.val){
                serverUrl = serverUrl+"&crowdGroup="+crowdGroup;
             }
        }
        var self = this;
        var sucFunc = function (result) {
            if (String(result.success) == 'true') {
                var effectData = result.resultObject;
                if(effectData){

                    // 处理逻辑,比较与默认的效果数据
                    var old = createActivityPublicParam.defaultParam;
                    if(isDefault){
                        old = effectData;
                    }else{
                        //effectData.isBetter = false;
                        var oldValue = parseFloat(old.tradeAmtRangeMax.replace(',',''));
                        var newValue = parseFloat(effectData.tradeAmtRangeMax.replace(',',''));
                        if(oldValue>newValue){
                            effectData.isBetter = false;
                        }else if(oldValue<newValue){
                            effectData.isBetter = true;
                        }else{
                            effectData.isBetter = null;
                        }
                    }
                    
                    self.redrawExpectEffectView(effectData);
                    return;
                }

            } else {
                DialogUtil.dialogWarn(result.msg);
            }
        };

        var failFunc = function () {
            log4JS.info("queryForecast failed. http status not 200");
            DialogUtil.dialogWarn();
        };
        CommonUtil.sendJsonpRequest({"url":serverUrl,"success":sucFunc,"error":failFunc});
        
    }


    /**
     * 绑定活动日期联动控件;  
     */
    this.bindVoucherTimeWidget = function(){    
        ActivityUtil.loadTimeWidget(true,function(){
            var activityDateParams = createActivityPublicParam.selectDateParams;
            activityDateParams.dateType = "voucherDate";
            activityDateParams.pageId = "smCreateConsumePage";
            activityDateParams.beginDateInputId = "voucher-date-start";
            activityDateParams.endDateInputId = "voucher-date-end";
            activityDateParams.selectBeginDateFunc = function(selectTime){
                if(selectTime != createActivityPublicParam.voucherStartTime){
                    createActivityPublicParam.voucherStartTime = selectTime;
                    createActivityPublicParam.voucherEndTime = DateUtil.getDateFormat(DateUtil.addDays(DateUtil.str2Date(selectTime),30));
                    $("#createXfsqActivityPage #voucher-date-end").val("");
                    ActivityUtil.loadTimeWidget(true);
                }
            };
            activityDateParams.selectEndDateFunc = function(selectTime){
                if(selectTime != createActivityPublicParam.voucherEndTime){
                    createActivityPublicParam.voucherEndTime = selectTime;
                    ActivityUtil.loadTimeWidget(true);
                }       
            };
            
        });
    }

    
};